export function timeFix () {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome () {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent () {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader (callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () { }
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE () {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate (id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}
export function scorePassword (pass) {
  let score = 0
  if (!pass) {
    return score
  }
  // award every unique letter until 5 repetitions
  const letters = {}
  for (let i = 0; i < pass.length; i++) {
    letters[pass[i]] = (letters[pass[i]] || 0) + 1
    score += 5.0 / letters[pass[i]]
  }

  // bonus points for mixing it up
  const variations = {
    digits: /\d/.test(pass),
    lower: /[a-z]/.test(pass),
    upper: /[A-Z]/.test(pass),
    nonWords: /\W/.test(pass)
  }

  let variationCount = 0
  for (var check in variations) {
    variationCount += (variations[check] === true) ? 1 : 0
  }
  score += (variationCount - 1) * 10

  return parseInt(score)
}

// 生成UUID
export function getUUID (len, radix) {
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  var uuid = []
  var i
  radix = radix || chars.length
  if (len) {
    for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
  } else {
    var r
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | Math.random() * 16
        uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r]
      }
    }
  }
  return uuid.join('')
}

// 使用递归的方式实现数组、对象的深拷贝
export function deepClone (obj) {
  // 判断拷贝的要进行深拷贝的是数组还是对象，是数组的话进行数组拷贝，对象的话进行对象拷贝
  var objClone = Array.isArray(obj) ? [] : {}
  // 进行深拷贝的不能为空，并且是对象或者是
  if (obj && typeof obj === 'object') {
    for (const key in obj) {
      if (obj.hasOwnProperty(key)) {
        if (obj[key] && typeof obj[key] === 'object') {
          objClone[key] = deepClone(obj[key])
        } else {
          objClone[key] = obj[key]
        }
      }
    }
  }
  return objClone
}

// 递归过滤函数，根据条件过滤数组对象
export function filterData (data, condition) {
  return data.filter(item => {
    if (condition(item)) {
      // 如果满足条件，则保留该项
      return true
    } else if (item.children && item.children.length > 0) {
      // 如果有子项，则递归调用过滤函数对子项进行过滤
      item.children = filterData(item.children, condition)
      // 如果子项中有满足条件的项，则保留该父项
      return item.children.length > 0
    } else {
      // 否则不保留该项
      return false
    }
  })
}

// 遍历树形结构数据下的所有子节点的id
export function getStorageIds (array) {
  let ids = []
  function getIds (array, ids) {
    array.forEach(item => {
      if (item.permissionId && item.resourceType == 2) {
        ids.push(item.permissionId)
      }
      if (item.children) {
        ids = getIds(item.children, ids)
      }
    })
    return ids
  }
  if (Array.isArray(array)) {
    if (array.length == 0) {
      return []
    }
  } else {
    return []
  }
  ids = getIds(array, ids)
  return ids
}

// 根据子级id得到父级id
export function getSelectedItem (value, data) {
  let str = ''
  const _this = this
  data.some((d) => {
    return d.children?.some((c) => {
      // if (c.permissionId == value && c.resourceTypeName != 1) {
      //   str = d.permissionId + ',' + c.permissionId;
      //   return true;
      // }
      if (c.permissionId == value && c.resourceTypeName != 1) {
        str = d.permissionId + ''
        return true
      } else if (c.resourceTypeName == 1) {
        str = c.permissionId + ''
        return true
      } else {
        if (c.children.length > 0) {
          getSelectedItem(value, c)
        }
      }
    })
  })
  return str
}

// 得到所有的子节点
export function lookId (data = [], type, arr = []) {
  for (var i = 0; i < data.length; i++) {
    // 获取除了父级的所有id
    if (type == 'allChildrenId') {
      if (data[i].resourceType != 1) arr.push(data[i].permissionId)
    }
    // 获取父级id
    else if (type == 'parentId') {
      if (data[i].resourceType == 1) arr.push(data[i].permissionId)
    }
    // 只获取页面id，不获取父级id和操作类型的id
    else if (type == 'pageId') {
      if (data[i].resourceType == 2) arr.push(data[i].permissionId)
    }
    // 只获取按钮权限
    else if (type == 'operation') {
      if (data[i].resourceType == 3) arr.push(data[i].permissionId)
    }
    // 获取所有id
    else {
      arr.push(data[i].permissionId)
    }
    if (data[i].children && data[i].children.length) {
      lookId(data[i].children, type, arr)
    }
  }
  return arr
}

// 过滤掉资源为操作和接口类型
export function ToTree (list) {
  list.forEach(item => {
    if (item.resourceType == 1) {
      item.component = 'RouteView'
    }
    // 菜单项提取，按钮、接口类型不显示
    if ((item.children.length > 0 && item.resourceType == 1)) {
      ToTree(item.children)
    } else {
      item.children = []
    }
  })
  return list
}
